package games.checkers;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JPanel;

public class chipBin extends JPanel 
{
	private Dimension d;
	private checkers program;
	private byte player;
	private int index;
	private GradientPaint background;
	
	public chipBin(checkers p, byte c)
	{
		;setBackground(new Color(160,82,45));
		program = p;
		player = c; 
		d = new Dimension(50,512);
		setSize(d);
		setPreferredSize(d);
		
		if (player == program.chipManager().player1) 
			background = new GradientPaint(0,0,new Color(160,82,45),0+d.width,0+d.height,new Color(80,41,23));
		else 
			background = new GradientPaint(0+d.width,0+d.height,new Color(160,82,45),0,0,new Color(80,41,23));
	}
	
	public void reset()
	{
		index = 0;
	}
	
	public Point binSpot()
	{
		Point p = new Point();
		index++;
		int start = -16;
		int step = 1; 
		if (player == program.chipManager().player1) 
		{
			start += 550;
			step = -1;
		}
		
		p.x = 28;
		p.y = step*index*42+start;
		
		return p;
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		
		//reset to background color
//		g2d.setColor(Color.white);
		g2d.setPaint(background);
		g2d.fillRect(0,0, d.width-1,d.height-1);
//		g2d.setColor(Color.white);
//		g2d.setStroke(new BasicStroke(2));
//		g.drawRect(0,0, d.width,d.height);
		
		// draw the chips on the board
		if (player == chipManager.player1)
		{
		ArrayList<?> redChips = program.chipManager().getRedChips();
		chip rChip;
		for (int i=redChips.size()-1; i>=0; --i)
		{
			rChip = (chip) redChips.get(i);
			if (!rChip.onBoard()) rChip.paint(g2d);
		}
		}
		else
		{
		ArrayList<?> whiteChips = program.chipManager().getWhiteChips();
		chip wChip;
		for (int i=whiteChips.size()-1; i>=0; --i)
		{
			wChip = (chip) whiteChips.get(i);
			if (!wChip.onBoard()) wChip.paint(g2d);
		}
		}
	}
}
